{include file="public/layout" /}
<body class="eyou_body_xin">
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
{include file="shop_product/left" /}
<div id="geduan_div" class="h15"></div>

<div id="page_div" class="page min-hg-c mb-20" style="min-width: auto; box-shadow: none; margin-left: 115px;padding-bottom: 15px;">
    <div class="flexigrid flexigrid-xin" style="position: unset !important;overflow: unset;">
        <div class="mDiv">
            <div class="ftitle">
               {eq name="$Think.const.CONTROLLER_NAME.'@add'|is_check_access" value="1"}
               <div class="fbutton" style="float: none;">
                   <a href="{:url('ShopProduct/add', ['source'=>'add'])}"><div class="add"><span><i class="layui-icon layui-icon-addition"></i>发布商品</span></div></a>
               </div>
               {/eq}
            </div>
        </div>
        <div class="fixed-bar">
            <div class="item-title">
                <ul class="tab-base nc-row">
                    <li> <a href='{:url("ShopProduct/index", ["query"=>1])}' {if condition='"index" == $Request.param.a && 1 == $param.query'} class="current" {/if}>出售中</a> </li>
                    <li> <a href='{:url("ShopProduct/index", ["query"=>2])}' {if condition='"index" == $Request.param.a && 2 == $param.query'} class="current" {/if}>仓库中</a> </li>
                    <li> <a href='{:url("ShopProduct/index", ["query"=>3])}' {if condition='"index" == $Request.param.a && 3 == $param.query'} class="current" {/if}>库存预警</a> </li>
                    <li> <a href='{:url("ShopProduct/index", ["query"=>4])}' {if condition='"index" == $Request.param.a && 4 == $param.query'} class="current" {/if}>回收站</a> </li>
                </ul>
                <div class="fr e-export">
                    <a href="javascript:void(0);" onclick="ProductExcelExport(this);" data-url="{:url('Order/ajax_order_excel_export', ['export_type'=>7])}" class=""><i class="iconfont e-XLStubiao"></i>导出商品</a>
                </div>
            </div>
        </div>
        <div class="header-bar header-bj">
            <div class="more">
                <div class="search">
                    <div class="content" >
                        <form id="searchForm" action="{:url('ShopProduct/index')}" method="get" onsubmit="layer_loading('正在处理');" style="display: unset;">
                            {$searchform.hidden|default=''}
                            <input type="hidden" id="query" name="query" value="{$param.query}">
                            <div class="ey-form-item"><label class="users-form-item-label">商品名称：</label>
                                <div class="users-form-item-content">
                                    <div class="users-select">
                                        <div tabindex="0" class="users-select-selection">
                                            <div class="sDiv2">
                                                <input type="text" size="50" name="title" id="title" value="{$param.title}" class="qsbox" style="width: 170px;" placeholder="输入商品名称搜索...">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="ey-form-item"><label class="users-form-item-label">商品标签：</label>
                                <div class="users-form-item-content">
                                    <div class="users-select">
                                        <div tabindex="0" class="users-select-selection">
                                            <div class="">
                                                <select name="label_types_id" id="label_types_id">
                                                    <option value="" {empty name="$param.label_types_id"}selected{/empty}>全部</option>
                                                    {volist name="$labelTypes" id="vo"}
                                                    <option value="{$vo.types_id}" {eq name="$param.label_types_id" value="$vo.types_id"}selected{/eq}>{$vo.types_names}</option>
                                                    {/volist}
                                                </select>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
    
                            <div class="ey-form-item " style="position: relative !important;">
                                <label class="users-form-item-label">商品分类：</label>
                                <div class="users-form-item-content" id="goodsStypeid">
                                    <div class="users-select">
                                        <div class="users-select-selection">
                                            <div class="sDiv2 ">
                                                <input type="text" class="qsbox fenlei" placeholder="{$stypeidCount|default='全部'}" lay-key="1" style="width: 210px;" id="stypeidCount">
                                                <i class=" layui-icon-down layui-icon" style="position: absolute;top: 9px;right: 10px;color: #444343; font-size: 10px;font-weight: bold;"></i>
                                            </div>
                                        </div>
                                        <div id="ey_fenleitanchuang" style="display: none;" onselectstart="return false;">
                                            <table class="flex-table autoht" cellpadding="0" cellspacing="0" border="0" id="arctype_table" style="width: 100%">
                                                <tbody id="treet1">
                                                {foreach name="arctype_list" item="vo" key="k" }
                                                    <tr nctype="0" {if condition="!in_array($vo.parent_id,$tree.cookied_treeclicked) && 0 < $vo.parent_id "} style="display:none;"{/if} class="parent_id_{$vo['parent_id']}" data-level="{$vo['level']}" data-id="{$vo['id']}">
                                                        <td class="typename" style="width: 100%">
                                                            <div style="margin: 10px 0;">
                                                                {eq name='vo.level' value='1'}<span class="" style="margin-left: 20px;"></span>{/eq}
                                                                {egt name='vo.level' value='2'}<span class="" style="margin-left: 40px;"></span>{/egt}
                                                                {gt name="vo.has_children" value="0"}
                                                                <img src="{in name='$vo.id' value='$tree.cookied_treeclicked'}__SKIN__/images/tv-collapsable-last.gif{else /}__SKIN__/images/tv-expandable.gif{/in}" style="float: none;" fieldid="2" status="open" nc_type="flex" onclick="treeClicked(this, {$vo['id']}, 0);" class="has_children childrenImg" data-level="{$vo['level']}" data-id="{$vo['id']}">
                                                                {else /}
                                                                <img src="__SKIN__/images/tv-collapsable-last.gif" style="float: none;" fieldid="2" status="open" nc_type="flex" class="childrenImg">
                                                                {/gt}
                                                                <label><input type="checkbox" name="stypeid[]" value="{$vo.id}" id="{$vo.id}" data-level="{$vo.level}" data-topid="{$vo.topid}" data-parent_id="{$vo.parent_id}" data-typename="{$vo.typename}" data-has_children="{$vo.has_children}" onclick="selectStypeid(this);" class="stypeid {eq name='$vo.level' value='1'} topid_{$vo.topid} {/eq}">&nbsp;{$vo.typename}</label>
                                                            </div>
                                                        </td>
                                                    </tr>
                                                {/foreach}
                                                </tbody>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="ey-form-item"><label class="users-form-item-label">创建时间：</label>
                                <div class="users-form-item-content">
                                    <div class="users-select">
                                        <div tabindex="0" class="users-select-selection">
                                            <div class="sDiv2">
                                                <input type="text" name="add_time" id="eYLaydateTimeLinkage" data-max_date="{php}echo date('Y-m-d');{/php}" autocomplete="off"  class="qsbox" placeholder="商品创建时间" lay-key="1" style="width: 220px;" value="{$param.add_time}">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
    
                            <div>&nbsp;</div>
                            <div class="ey-form-item"><label class="users-form-item-label">商品销量：</label>
                                <div class="users-form-item-content">
                                    <div class="users-select">
                                        <div tabindex="0" class="users-select-selection">
                                            <div class="sDiv2">
                                                <input type="text" name="sales_num1" {notempty name="$param.sales_num1"} value="{$param.sales_num1}" {/notempty} class="qsbox" placeholder="最小值" onkeyup="this.value=this.value.replace(/\D/g,'')">
                                            </div>
                                            &nbsp;至&nbsp;
                                            <div class="sDiv2">
                                                <input type="text" name="sales_num2" {notempty name="$param.sales_num2"} value="{$param.sales_num2}" {/notempty} class="qsbox" placeholder="最大值" onkeyup="this.value=this.value.replace(/\D/g,'')">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
    
                            <div class="ey-form-item"><label class="users-form-item-label">商品价格：</label>
                                <div class="users-form-item-content">
                                    <div class="users-select">
                                        <div tabindex="0" class="users-select-selection">
                                            <div class="sDiv2">
                                                <input type="text" name="users_price1" {notempty name="$param.users_price1"} value="{$param.users_price1}" {/notempty} class="qsbox" placeholder="最小值" onkeyup="this.value=this.value.replace(/\D/g,'')">
                                            </div>
                                            &nbsp;至&nbsp;
                                            <div class="sDiv2">
                                                <input type="text" name="users_price2" {notempty name="$param.users_price2"} value="{$param.users_price2}" {/notempty} class="qsbox" placeholder="最大值" onkeyup="this.value=this.value.replace(/\D/g,'')">
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </form>
                        <div class="ey-form-item" style="margin-left: 20px;">
                            <div class="users-form-item-content">
                                <span class="bt-xin" id="submitForm" onclick="productQuery();">查询</span>
                                <span class="bt-xin-no" onclick="productQuery('reset');">重置</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="geduan-xin"></div>
    <div class="flexigrid flexigrid2">
        <div class="bDiv" style="height: auto;">
            <div id="flexigrid" cellpadding="0" cellspacing="0" border="0">
                {empty name="list"}
                <table style="width: 100%;">
                    <thead style="background-color: #f2f2f2; line-height: 40px;">
                        <tr>
                            <th class="sign w50" axis="col0"> <div class="tc"> <input type="checkbox" autocomplete="off" class="checkAll"> </div> </th>
                            <th abbr="article_title" axis="col3" class="w60"> <div class="tc">名称</div> </th>
                            <th abbr="article_title" axis="col3" class=""> <div class="ml15"></div> </th>
                            <th abbr="article_title" axis="col3" class="w80">
                                <div class="tc sort_style">
                                    <a href="{:getArchivesSortUrl('users_price')}">价格&nbsp;<i {eq name='$Request.param.orderby' value='users_price'}{eq name='$Request.param.orderway' value='asc'}class="asc"{else /}class="desc"{/eq}{/eq}></i></a>
                                </div>
                            </th>
                            <th abbr="article_title" axis="col3" class="w80">
                                <div class="tc sort_style">
                                    <a href="{:getArchivesSortUrl('stock_count')}">库存&nbsp;<i {eq name='$Request.param.orderby' value='stock_count'}{eq name='$Request.param.orderway' value='asc'}class="asc"{else /}class="desc"{/eq}{/eq}></i></a>
                                </div>
                            </th>
                            <th abbr="article_title" axis="col3" class="w80">
                                <div class="tc sort_style">
                                    <a href="{:getArchivesSortUrl('sales_num')}">实际销量&nbsp;<i {eq name='$Request.param.orderby' value='sales_num'}{eq name='$Request.param.orderway' value='asc'}class="asc"{else /}class="desc"{/eq}{/eq}></i></a>
                                </div>
                            </th>
                            <th axis="article_title" axis="col3" class="w160"> <div class="tc">创建时间</div> </th>
                            <!-- <th axis="article_title" axis="col3" class="w120"> <div class="tc">商品标签</div> </th> -->
                            <th axis="article_title" axis="col3" class="w220"> <div class="tc">操作</div> </th>
                            <th abbr="article_title" axis="col6" class="w60">
                                <div class="tc sort_style"><a href="{:getArchivesSortUrl('sort_order')}">排序&nbsp;<i {eq name='$Request.param.orderby' value='sort_order'}{eq name='$Request.param.orderway' value='asc'}class="asc"{else /}class="desc"{/eq}{/eq}></i></a></div>
                            </th>
                        </tr>
                    </thead>
                </table>
                <div class="no_row">
                    <div class="no_pic"><img src="__SKIN__/images/null-data.png"></div>
                </div>
                {else/}
                    
                <table style="width: 100%;">
                    <thead style="background-color: #f2f2f2; line-height: 40px;">
                        <tr>
                            <th class="sign w50" axis="col0"> <div class="tc"> <input type="checkbox" autocomplete="off" class="checkAll"> </div> </th>
                            <th abbr="article_title" axis="col3" class="w60"> <div class="tc">名称</div> </th>
                            <th abbr="article_title" axis="col3" class=""> <div class="ml15"></div> </th>
                            <th abbr="article_title" axis="col3" class="w80">
                                <div class="tc sort_style">
                                    <a href="{:getArchivesSortUrl('users_price')}">价格&nbsp;<i {eq name='$Request.param.orderby' value='users_price'}{eq name='$Request.param.orderway' value='asc'}class="asc"{else /}class="desc"{/eq}{/eq}></i></a>
                                </div>
                            </th>
                            <th abbr="article_title" axis="col3" class="w80">
                                <div class="tc sort_style">
                                    <a href="{:getArchivesSortUrl('stock_count')}">库存&nbsp;<i {eq name='$Request.param.orderby' value='stock_count'}{eq name='$Request.param.orderway' value='asc'}class="asc"{else /}class="desc"{/eq}{/eq}></i></a>
                                </div>
                            </th>
                            <th abbr="article_title" axis="col3" class="w80">
                                <div class="tc sort_style">
                                    <a href="{:getArchivesSortUrl('sales_num')}">实际销量&nbsp;<i {eq name='$Request.param.orderby' value='sales_num'}{eq name='$Request.param.orderway' value='asc'}class="asc"{else /}class="desc"{/eq}{/eq}></i></a>
                                </div>
                            </th>
                            <th axis="article_title" axis="col3" class="w160"> <div class="tc">创建时间</div> </th>
                            <!-- <th axis="article_title" axis="col3" class="w120"> <div class="tc">商品标签</div> </th> -->
                            <th axis="article_title" axis="col3" class="w220"> <div class="tc">操作</div> </th>
                            <th abbr="article_title" axis="col6" class="w60">
                                <div class="tc sort_style"><a href="{:getArchivesSortUrl('sort_order')}">排序&nbsp;<i {eq name='$Request.param.orderby' value='sort_order'}{eq name='$Request.param.orderway' value='asc'}class="asc"{else /}class="desc"{/eq}{/eq}></i></a></div>
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        {foreach name="list" item="vo" key="k" }
                            <tr>
                                <td class="sign"> <div class="tc w50"> <input type="checkbox" autocomplete="off" name="aids[]" value="{$vo.aid}"> </div> </td>
                                <td class="w60">
                                    <div class="tc">
                                        <a href="{:url('ShopProduct/edit', ['id'=>$vo['aid'], 'source'=>'add'])}">
                                            <img width="60" height="60" src="{$vo.litpic}">
                                        </a>
                                    </div>
                                </td>
                                <td class="goods-item">
                                    <div class="fl" style="padding-left: 10px;">
                                       <div class="title">
                                            {eq name="$Think.const.CONTROLLER_NAME.'@edit'|is_check_access" value="1"}
                                                <a href="{:url('ShopProduct/edit', ['id'=>$vo['aid'], 'source'=>'add'])}">{$vo.title}</a>
                                            {else /}
                                                {$vo.title}
                                            {/eq}
                                            {notempty name="$vo.label_names"} <br><span class="pro-span">{$vo.label_names}</span> {/notempty}
                                       </div>
                                    </div>
                                </td>
                                <td class="w80"> <div class="tc">￥{$vo.users_price}</div> </td>
                                <td class="w80"> <div class="tc" {eq name="$param.query" value="3"}style="color: red;"{/eq}>{$vo.stock_count}</div> </td>
                                <td class="w80"> <div class="tc">{$vo.sales_num}</div> </td>
                                <td class="w160"> <div class="tc">{$vo.add_time|date='Y-m-d H:i:s',###}</div> </td>
                                <!-- <td class="w120"> <div class="tc">{$vo.label_names}</div> </td> -->
                                <td align="center" class="operation">
                                    <div class="tc">
                                    {eq name="$param.query" value="4"}
                                        <a href="JavaScript:void(0);" onclick="productRestore({$vo.aid});" class="btn blue">恢复</a><i></i>
                                    {else/}
                                        <a href="JavaScript:void(0);" onclick="productStatus({$vo.aid}, 0);" class="btn blue" id="status0_{$vo.aid}" {eq name="$vo.status" value="0"} style="display: none;" {/eq}>下架</a>
                                        <a href="JavaScript:void(0);" onclick="productStatus({$vo.aid}, 1);" class="btn blue" id="status1_{$vo.aid}" {eq name="$vo.status" value="1"} style="display: none;" {/eq}>上架</a>
                                        <i></i>
                                        <!-- {eq name="$param.query" value="1"}
                                        <a href="JavaScript:void(0);" onclick="productPromote({$vo.aid});" class="btn blue">推广</a><i></i>
                                        {/eq} -->
                                        <a href="JavaScript:void(0);" onclick="productCopy({$vo.aid});" class="btn blue">复制</a><i></i>
                                    {/eq}
                                        {eq name="$Think.const.CONTROLLER_NAME.'@edit'|is_check_access" value="1"}
                                        <a href="{:url('ShopProduct/edit', ['id'=>$vo['aid'], 'source'=>'add'])}" class="btn blue">编辑</a><i></i>
                                        {/eq}
                                        {eq name="$Think.const.CONTROLLER_NAME.'@del'|is_check_access" value="1"}
                                        <a href="javascript:void(0);" onclick="productDelete({$vo.aid});" class="btn red">删除</a>
                                        {/eq}
                                    </div>
                                </td>
                                <td class="sort">
                                    <div class="w60 tc">
                                        {eq name="$Think.const.CONTROLLER_NAME.'@edit'|is_check_access" value="1"}
                                            <input type="text" onchange="changeTableVal('archives', 'aid', '{$vo.aid}', 'sort_order', this);" size="4" value="{$vo.sort_order}" onkeyup="this.value=this.value.replace(/[^\d]/g,'');" onpaste="this.value=this.value.replace(/[^\d]/g,'')"/>
                                        {else /}
                                            {$vo.sort_order}
                                        {/eq}
                                    </div>
                                </td>
                            </tr>
                            {/foreach}
                        </tbody>
                    </table>
                    {/empty}
                </div>
                <div class="iDiv" style="display: none;"></div>
            </div>
            {notempty name="list"}
            <div class="footer-oper">
                <span class="ml20"> <input type="checkbox" autocomplete="off" class="checkAll"> </span>
                <div class="nav-dropup">
                    <button class="layui-btn layui-btn-primary dropdown-bt">批量操作<i class="layui-icon layui-icon-up"></i></button>
                    <div class="dropdown-menus" style="display:none; {if condition='0 < $pager->totalRows && ($pager->totalRows < 2 || $pager->listRows < 2)'}top:28px;bottom:unset;border-bottom:1px solid rgba(0,0,0,.15);border-top:none;min-height: 250px;{/if}">
                        {eq name="$param.query" value="4"}
                        <a href="javascript:void(0);" onclick="productRestore(null);">恢复</a>
                        {else/}
                        <a href="javascript:void(0);" onclick="productStatus(null, 1);">上架</a>
                        <a href="javascript:void(0);" onclick="productStatus(null, 0);">下架</a>
                        {/eq}
                        <a href="javascript:void(0);" onclick="productDelete(null);">删除</a>
                    </div>
                </div>
                {include file="public/page" /}
            </div>
            {/notempty}
        </div>
    </div>
</div>

<script type="text/javascript">
    
    $(function() {

        $(document).keydown(function(event){
            if (event.keyCode ==13) {
                $('#searchForm input[type=text]').each(function(index, item){
                    if ($(item).is(':focus')) {
                        $('#searchForm').submit();
                    }
                });
            }
        });

        $('input[name*=aids]').click(function() {
            console.log($('input[name*=aids]').length);
            console.log($('input[name*=aids]:checked').length);
            if ($('input[name*=aids]').length == $('input[name*=aids]:checked').length) {
                $('.checkAll').prop('checked', 'checked');
            } else {
                $('.checkAll').prop('checked', false);
            }
        });
        $('input[type=checkbox].checkAll').click(function() {
            $('input[type=checkbox]').prop('checked', this.checked);
        });
    });

    $(document).ready(function() {
        // 表格行点击选中切换
        $('#flexigrid > table>tbody >tr').click(function() {
            $(this).toggleClass('trSelected');
        });

        // 点击刷新数据
        $('.fa-refresh').click(function() {
            location.href = location.href;
        });

        // 批量操作
        $(".dropdown-bt").click(function(){
            $(".dropdown-menus").slideToggle(200);
            event.stopPropagation();
        })
        $(document).click(function(){
            $(".dropdown-menus").slideUp(200);
            event.stopPropagation();
        })
    });

    // 商品查询搜索
    function productQuery(isReset) {
        if (isReset && 'reset' == isReset) {
            window.location.href = "{:url('ShopProduct/index', ['query'=>$Request.param.query])}";
        } else {
            $('#searchForm').submit();
        }
    }

    // 商品状态更新
    function productStatus(aid, status) {
        if (!aid || aid === null) {
            var aids = [];
            $('input[name^=aids]').each(function(i, o) {
                if ($(o).is(':checked')) aids.push($(o).val());
            });
            if (aids.length == 0) {
                layer.alert('请至少选择一项', {icon: 5, title: false, closeBtn: 0});
                return false;
            }
        } else {
            aids = [aid];
        }

        // 执行更新
        var title = 1 === parseInt(status) ? '上架' : '下架';
        layer.alert('确定' + title + '该商品？', {
            move: false,
            closeBtn: 3,
            btnAlign:'r',
            title: '提示',
            shade: layer_shade,
            btn: ['确定', '取消'],
            area: ['480px', '190px'],
            success: function () {
                $(".layui-layer-content").css('text-align', 'left');
            }
        }, function() {
            layer_loading('正在处理');
            $.ajax({
                type: 'post',
                url : "{:url('ShopProduct/product_status')}",
                data: {aid: aids, status: status, _ajax: 1},
                dataType: 'json',
                success: function(res) {
                    layer.closeAll();
                    if (1 === parseInt(res.code)) {
                        layer.msg(res.msg, {icon: 1, time: 1500}, function() {
                            window.location.reload();
                        });
                    } else {
                        // 提示内容
                        showErrorMsg(res.msg);
                    }
                }
            });
        });
    }

    // 商品删除(query=1，2，3回收站、 query=4彻底删除)
    function productDelete(aid) {
        if (!aid || aid === null) {
            var aids = [];
            $('input[name^=aids]').each(function(i, o) {
                if ($(o).is(':checked')) aids.push($(o).val());
            });
            if (aids.length == 0) {
                layer.alert('请至少选择一项', {icon: 5, title: false, closeBtn: 0});
                return false;
            }
        } else {
            aids = [aid];
        }

        // 执行更新
        var query = $('#query').val();
        var title = 4 === parseInt(query) ? '确定彻底删除该商品？' : '确认将该商品放入回收站？';
        layer.alert(title, {
            move: false,
            closeBtn: 3,
            btnAlign:'r',
            title: '提示',
            shade: layer_shade,
            btn: ['确定', '取消'],
            area: ['480px', '190px'],
            success: function () {
                $(".layui-layer-content").css('text-align', 'left');
            }
        }, function() {
            layer_loading('正在处理');
            $.ajax({
                type: 'post',
                url : "{:url('ShopProduct/product_delete')}",
                data: {aid: aids, query: query, _ajax: 1},
                dataType: 'json',
                success: function(res) {
                    layer.closeAll();
                    if (1 === parseInt(res.code)) {
                        layer.msg(res.msg, {icon: 1, time: 1500}, function() {
                            window.location.reload();
                        });
                    } else {
                        // 提示内容
                        showErrorMsg(res.msg);
                    }
                }
            });
        });
    }

    // 商品恢复(从伪删除恢复到原先状态)
    function productRestore(aid) {
        if (!aid || aid === null) {
            var aids = [];
            $('input[name^=aids]').each(function(i, o) {
                if ($(o).is(':checked')) aids.push($(o).val());
            });
            if (aids.length == 0) {
                layer.alert('请至少选择一项', {icon: 5, title: false, closeBtn: 0});
                return false;
            }
        } else {
            aids = [aid];
        }

        // 执行更新
        var title = '确定恢复该商品？';
        layer.alert(title, {
            move: false,
            closeBtn: 3,
            btnAlign:'r',
            title: '提示',
            shade: layer_shade,
            btn: ['确定', '取消'],
            area: ['480px', '190px'],
            success: function () {
                $(".layui-layer-content").css('text-align', 'left');
            }
        }, function() {
            layer_loading('正在处理');
            $.ajax({
                type: 'post',
                url : "{:url('ShopProduct/product_restore')}",
                data: {aid: aids, _ajax: 1},
                dataType: 'json',
                success: function(res) {
                    layer.closeAll();
                    if (1 === parseInt(res.code)) {
                        layer.msg(res.msg, {icon: 1, time: 1500}, function() {
                            window.location.reload();
                        });
                    } else {
                        // 提示内容
                        showErrorMsg(res.msg);
                    }
                }
            });
        });
    }

    // 商品推广
    function productPromote(aid) {
        var imgHtml = '<img src="__SKIN__/images/applet_img.jpg">';
        layer.alert(imgHtml, {
            move: false,
            closeBtn: 3,
            btnAlign:'r',
            title: '商品推广',
            shade: layer_shade,
            btn: false,
            area: ['400px', '450px'],
            success: function () {
                $(".layui-layer-content").css('text-align', 'left');
            }
        });
    }

    // 商品复制
    function productCopy(aid) {
        if (!aid || aid === null) {
            var aids = [];
            $('input[name^=aids]').each(function(i, o) {
                if ($(o).is(':checked')) aids.push($(o).val());
            });
            if (aids.length == 0) {
                layer.alert('请至少选择一项', {icon: 5, title: false, closeBtn: 0});
                return false;
            }
        } else {
            aids = [aid];
        }

        // 执行更新
        var title = '确定复制该商品？';
        layer.alert(title, {
            move: false,
            closeBtn: 3,
            btnAlign:'r',
            title: '提示',
            shade: layer_shade,
            btn: ['确定', '取消'],
            area: ['480px', '190px'],
            success: function () {
                $(".layui-layer-content").css('text-align', 'left');
            }
        }, function() {
            layer_loading('正在处理');
            $.ajax({
                type: 'post',
                url : "{:url('ShopProduct/product_copy')}",
                data: {aid: aids, _ajax: 1},
                dataType: 'json',
                success: function(res) {
                    layer.closeAll();
                    if (1 === parseInt(res.code)) {
                        layer.msg(res.msg, {icon: 1, time: 1500}, function() {
                            window.location.reload();
                        });
                    } else {
                        // 提示内容
                        showErrorMsg(res.msg);
                    }
                }
            });
        });
    }
</script>

<!-- 日期联动选择js文件 start -->
{eyou:static file="__PUBLIC__/static/common/js/ey_laydate_time_linkage.js"/}
<!-- 日期联动选择js文件 end -->
{include file="public/footer" /}

<input type="hidden" id="post_stypeid" value="{$stypeidStr}">

<script type="text/javascript">
    $(function() {
        var stypeidStr = $('#post_stypeid').val();
        var stypeidArr = stypeidStr.split(',');
        $('.stypeid').each(function() {
            var _this = $(this);
            var index = $.inArray(String(_this.val()), stypeidArr);
            if (parseInt(index) > -1) {
                _this.attr('checked', true);
                $('#' + _this.data('topid')).attr('checked', true);
                $('#' + _this.data('parent_id')).attr('checked', true);
            }
        });

        $('#goodsStypeid').mouseover(function() {
            $('#ey_fenleitanchuang').show();
        });
        $('#goodsStypeid').mouseout(function() {
            $('#ey_fenleitanchuang').hide();
        });
    });

    function selectStypeid(obj) {
        var isSelect = $(obj).prop('checked');
        var selectID = parseInt($(obj).val());
        var selectLevel = parseInt($(obj).data('level'));
        var selectTopID = parseInt($(obj).data('topid'));
        var selectParentID = parseInt($(obj).data('parent_id'));
        var selectValue1 = selectValue2 = selectValue3 = 0;
        var postStypeidArr = [];
        $('.stypeid').each(function() {
            var _this = $(this);
            if (0 === selectLevel) {
                if (selectID === parseInt(_this.data('topid'))) _this.attr('checked', isSelect);
            } else if (1 === selectLevel) {
                if (selectID === parseInt(_this.data('parent_id'))) _this.attr('checked', isSelect);
            }
            if (1 === selectLevel && 1 === parseInt(_this.data('level')) && selectTopID === parseInt(_this.data('topid'))) {
                if (true === _this.prop('checked')) selectValue1++;
                if (0 === parseInt(selectValue1)) {
                    $('#' + selectTopID).attr('checked', false);
                } else {
                    $('#' + selectTopID).attr('checked', true);
                }
            }
            if (2 === selectLevel && 2 === parseInt(_this.data('level')) && selectParentID === parseInt(_this.data('parent_id'))) {
                if (true === _this.prop('checked')) selectValue2++;
                if (0 === parseInt(selectValue2)) {
                    $('#' + selectParentID).attr('checked', false);
                } else {
                    $('#' + selectTopID).attr('checked', true);
                    $('#' + selectParentID).attr('checked', true);
                }
            }

            // 选中的分类ID加入数组
            if (0 === _this.data('has_children') && true === _this.prop('checked')) postStypeidArr.push(_this.val());
        });

        // 处理二级分类是否选中
        $('.topid_' + selectTopID).each(function() {
            if (true === $(this).prop('checked')) selectValue3++;
        });
        if (0 === parseInt(selectValue3)) $('#' + selectTopID).attr('checked', false);

        // 所有选中的分类数量
        var selectStypeidLength = $('#arctype_table input[name="stypeid[]"]:checked').length;
        if (parseInt(selectStypeidLength) > 0) {
            $('#stypeidCount').attr('placeholder', '已选中 ' + selectStypeidLength + ' 个分类');
        } else {
            $('#stypeidCount').attr('placeholder', '全部');
        }

        // 选中的分类ID数组转字符串
        var postStypeidStr = postStypeidArr.join(',');
        $('#post_stypeid').val(postStypeidStr);
    }
</script>

<script type="text/javascript">
    $(function() {
        setCookies('stypeid-treeClicked-Arr', JSON.stringify({$tree.cookied_treeclicked_arr}) );
    });

    // 点击展开 收缩节点
    function treeClicked(obj, id, reload) {
        if (id == 'all') {
            if (1 == reload) {
                var status = getCookie('stypeid-treeClicked');
                if (!status) status = $(obj).attr('data-status');
            } else {
                var status = $(obj).attr('data-status');
            }
            if (status == 'close') {
                setCookies('stypeid-treeClicked-Arr', JSON.stringify({$tree.parent_ids}));
                setCookies('stypeid-treeClicked_All', 1);//1为全部展开,0-为非全部展开
                $('tr[class^=parent_id_]').show();
                $('.childrenImg').attr('src', '__SKIN__/images/tv-collapsable-last.gif');
                $(obj).attr('data-status', 'open').attr('title','关闭所有子分类').attr('src','__SKIN__/images/tv-collapsable-last.gif');
            } else {
                setCookies('stypeid-treeClicked-Arr', '');
                setCookies('stypeid-treeClicked_All', 0);
                $('tr[data-level=0]').find('img.has_children').attr('src', '__SKIN__/images/tv-collapsable-last.gif').trigger('click');
                $('tr[class^=parent_id_]').removeClass('trSelected');
                $(obj).attr('data-status', 'close').attr('title','展开所有子分类').attr('src','__SKIN__/images/tv-expandable.gif');
            }
            setCookies('stypeid-treeClicked', status);
            return false;
        }

        var src = $(obj).attr('src');
        if (src == '__SKIN__/images/tv-expandable.gif') {
            var arr = [];
            var str = getCookie('stypeid-treeClicked-Arr');
            if ('' == str || null == str || 'null' == str) {
                arr.push(id);
            } else {
                arr = JSON.parse(str);
                if (!arr.includes(id))arr.push(id);
            }
            arr = JSON.stringify(arr);
            setCookies('stypeid-treeClicked-Arr', arr);
            $(".parent_id_"+id).show();
            $(obj).attr('src','__SKIN__/images/tv-collapsable-last.gif');
            var status = 'close';
        } else {
            var key = -1;
            var arr = [];
            var level = '';
            var str = getCookie('stypeid-treeClicked-Arr');
            if ('' != str) {
                arr = JSON.parse(str);
                key = $.inArray(id,arr);
                if (-1 < key) arr.splice(key,1);
                level = $(obj).attr('data-level');
                $(obj).parent().parent().parent().nextAll().each(function() {
                    if ($(this).attr('data-level') > level) {
                        key = $.inArray(parseInt($(this).attr('data-id')), arr);
                        if (-1 < key) arr.splice(key,1);
                    } else {
                        return false;
                    }
                });
                arr = JSON.stringify(arr);
                setCookies('stypeid-treeClicked-Arr', arr);
                setCookies('stypeid-treeClicked_All', 0);
            }

            var status = 'open';
            $(obj).attr('src', '__SKIN__/images/tv-expandable.gif');     

            // 如果是点击减号, 遍历循环他下面的所有都关闭
            var fnd = false;
            var cur_tr = obj.parentNode.parentNode.parentNode;
            var tbl = document.getElementById("arctype_table");
            for (var i = 0; i < tbl.rows.length; i++) {
                var row = tbl.rows[i];
                if (row == cur_tr) {
                    fnd = true;
                } else {
                    if (fnd == true) {
                        var cur_level = $(cur_tr).data('level');
                        var level = parseInt($(row).data('level'));
                        if (level > cur_level) {
                            $(row).hide();        
                            $(row).find('img.has_children').attr('src','__SKIN__/images/tv-expandable.gif');
                        } else {
                            fnd = false;
                            break;
                        }
                    }
                }
            }
        }
        setCookies('stypeid-treeClicked', status);
    }

    // AJAX调用下载
    function ProductExcelExport(obj) {
        layer_loading('正在导出');
        $.ajax({
            type: 'post',
            url : $(obj).attr('data-url'),
            data: $('#searchForm').serialize(),
            dataType: 'json',
            success: function(res) {
                layer.closeAll();
                if (1 == res.code) {
                    window.location.href = res.url;
                } else {
                    showErrorAlert(res.msg);
                }
            },
            error: function(e) {
                layer.closeAll();
                showErrorAlert(e.responseText);
            }
        });
    }
</script>